<?php

$f3=require('../lib/base.php');
$f3->config('../config.ini');

$f3->route('GET /',
	function($f3) {
    $home = '';
		$f3->set('home',$home);

    $url = "http://android.stackexchange.com/";


    $req = \Web::instance()->request($url);

    $home = '';
    if ($req != null && isset($req['body'])) {
      $html  = $req['body'];
      $html = preg_replace('/[\r\n\t]/i','',$html);
      preg_match('/<div id="qlist-wrapper">[\s\S]*?<div><a id="home-browse"/i',$html, $home);
      $home = str_replace('<div><a id="home-browse"','',$home[0]);
      $home = preg_replace('/<div onclick="[\s\S]*?<div class="summary">/i','<div class="summary">',$home);
      $home = preg_replace('/<div class="started">[\s\S]*?<\/div>/i','',$home);
      $home = str_replace('post-tag', 'label label-info',$home);
      $home = preg_replace('/"\/questions\/([0-9]*)\/(.*?)"/i','"/questions/$1.html"',$home);
      $home = str_replace('/questions/tagged/','/tags/',$home);
    } 
    $f3->set('css','home.css');
    $f3->set('site_name','<title>Android Enthusiasts</title>');
    $f3->set('site_desc','<meta name="description" content="The site allow user find the question about android and developmen"/>');
    $f3->set('site_keywords','<meta name="keywords" content="gp958.com android question"/>');

    $f3->set('home',$home); 
		echo View::instance()->render('home.html');
           },5*3600
);

$f3->route('GET /questions/@id.html',

  function($f3) {
    $url = "http://android.stackexchange.com/questions/".$f3->get('PARAMS')["id"]."/";
    $req = \Web::instance()->request($url);

    $question = '';
    $qtitle   = '';
    $tags     = '';
    $qcontent = '';
    $answers  = '';
    $related  = '';

    if (isset($req['body'])) {

      $html = $req['body'];
      $html = preg_replace('/[\t\n\r]/i','',$html);
      preg_match('/<h1 itemprop="name">[\s\S]*<\/h1>/i',$html,$tmp);
      if (isset($tmp[0]) && $tmp[0] != "") {
        $qtitle = $tmp[0];

        $qtitle = preg_replace('/"\/questions\/([0-9]*)\/(.*)"/i','"/questions/$1.html"',$qtitle);
      }

      preg_match('/<div class="post-taglist">[\s\S]*?<\/div>/i',$html,$tmp);
      if (isset($tmp[0]) && count($tmp[0]) >0) {
        $tags = $tmp[0];
        $tags = str_replace('"post-tag"','"label label-info"',$tags);
        $tags= str_replace('/questions/tagged/','/tags/',$tags);
      }


      preg_match('/<div class="post-text" itemprop="description">[\s\S]*?<\/div>/i',$html,$tmp);

      if (isset($tmp[0])) {
        $qcontent = $tmp[0];
        $qcontent = str_replace('post-text', 'qcontent well',$qcontent);
      }

      preg_match('/<div class="related"[\s\S]*?<div id="feed-link">/i',$html,$rawr);

      if (isset($rawr[0])) {
        $rawr = str_replace('http://android.stackexchange.com','',$rawr);
         preg_match_all('/<a href="\/questions\/[0-9]*\/[\s\S]*?<\/a>/i',$html,$ras);

        $related = "<ul>";
        foreach($ras[0] as $a) {
          $related .= '<li><i class="icon-chevron-right"></i>'.preg_replace('/"\/questions\/([0-9]*)\/.*"/','"/questions/$1.html"',$a)."</li>"; 
        }

        $related .= "</ul>";

      }


      preg_match_all('/<td class="answercell">([\s\S]*?)<\/td>/i',$html,$tmp);

      if (count($tmp[1]) > 0) { 

      $answers = '<ul clsss="answers">';
       foreach($tmp[1] as $as) {
         $answers .= "<li>".$as."</li>";
         $answers = preg_replace('/<div class="post-menu">[\s\S]*?<\/div>/i', '', $answers);
       }
      $answers .= "</ul>";
      }


    }
    $f3->set('site_name','<title>Android Enthusiasts</title>');
    $f3->set('site_desc','<meta name="description" content="The site allow user find the question about android and developmen"/>');
    $f3->set('site_keywords','<meta name="keywords" content="gp958.com android question"/>');


    $f3->set('css','question.css');
    $f3->set('qtitle',$qtitle);
    $f3->set('qcontent',$qcontent);
    $f3->set('tags',$tags);
    $f3->set('answers',$answers);
    $f3->set('related',$related);
    echo View::instance()->render('question.html');

  },5*3600
);

$f3->route('GET /tags/*',
  function($f3) {
    $url = $f3->get('PARAMS')[0];
    $tag = explode('?',str_replace('/tags/','',$url))[0];
    $f3->set('qtitle',$tag);
    $tags = '';
    $related = '';
    $h3s = array();
    $excerpts = array();
    $tags = array();
    $host = "http://android.stackexchange.com/questions".str_replace('/tags/','/tagged/',str_replace(' ', '+', $f3->get('PARAMS')[0]));

    $req = \Web::instance()->request($host);

    if (isset($req['body'])) {

      $html = $req['body'];

      preg_match_all('/<h3>[\s\S]*?<\/h3>/i', $html,$tmp);
      if (count($tmp[0]) > 0)
        $h3s = $tmp[0];       

      preg_match_all('/<div class="excerpt">[\s\S]*?<\/div>/i', $html, $tmp);
      if (count($tmp[0]) > 0) 
        $excerpts = $tmp[0];


      preg_match_all('/<div class="tags[\s\S]*?">[\s\S]*?<\/div>/i', $html, $tmp);
      if (count($tmp[0]) > 0) 
        $tags = $tmp[0];

      preg_match_all('/<div class="module">[\s\S]*?<\/div>/i', $html, $tmp);
      if (isset($tmp[0][1]) && count($tmp[0][1]) > 0)
        $related = $tmp[0][1];
    }

    $f3->set('h3s',$h3s);
    $f3->set('excerpts',$excerpts);
    $f3->set('tags',$tags);
    $f3->set('related',$related);
    $f3->set('css','tag.css');
    $f3->set('site_name','<title>Android Enthusiasts</title>');
    $f3->set('site_desc','<meta name="description" content="The site allow user find the question about android and developmen"/>');
    $f3->set('site_keywords','<meta name="keywords" content="gp958.com android question"/>');



    echo View::instance()->render('tag.html');
  },2*3600
);

$f3->route('GET /questions*',
  function($f3) {

    $tags = '';
    $related = '';
    $h3s = array();
    $excerpts = array();
    $tags = array();
    $pagesl = '';
    $pagesr = '';
    $host = "http://android.stackexchange.com/".$f3->get('PARAMS')[0];

    $req = \Web::instance()->request($host);

    if (isset($req['body'])) {

      $html = $req['body'];

      preg_match_all('/<h3>[\s\S]*?<\/h3>/i', $html,$tmp);
      if (count($tmp[0]) > 0)
        $h3s = $tmp[0];       

      preg_match_all('/<div class="excerpt">[\s\S]*?<\/div>/i', $html, $tmp);
      if (count($tmp[0]) > 0) 
        $excerpts = $tmp[0];


      preg_match_all('/<div class="tags[\s\S]*?">[\s\S]*?<\/div>/i', $html, $tmp);
      if (count($tmp[0]) > 0) 
        $tags = $tmp[0];

      preg_match_all('/<div class="module" id="related-tags">[\s\S]*?<\/div>/i', $html, $tmp);
      if (isset($tmp[0][0]) && count($tmp[0][0]) > 0)
        $related = $tmp[0][0];

      preg_match('/<div class="pager fl"\s*>[\s\S]*?<\/div>/i', $html, $tmp);
      if (isset($tmp[0]))
        $pagesl = $tmp[0];

      preg_match('/<div class="page-sizer fr">[\s\S]*?<\/div>/i', $html, $tmp);
      if (isset($tmp[0]))
        $pagesr = $tmp[0];
    }

    $f3->set('h3s',$h3s);
    $f3->set('pagesl',$pagesl);
    $f3->set('pagesr',$pagesr);
    $f3->set('excerpts',$excerpts);
    $f3->set('tags',$tags);
    $f3->set('related',$related);

    $f3->set('css','questions.css');
    $f3->set('site_name','<title>Android Enthusiasts</title>');
    $f3->set('site_desc','<meta name="description" content="The site allow user find the question about android and developmen"/>');
    $f3->set('site_keywords','<meta name="keywords" content="gp958.com android question"/>');


    echo View::instance()->render('questions.html');
  },2*3600
);

$f3->route('GET /tags*',
  function ($f3) {
    $urls = $f3->get('PARAMS');
    $host = 'http://android.stackexchange.com'.$urls[0];

    $tags = '';
    $pages = '';

    $req = \Web::instance()->request($host);

    if (isset($req['body'])) {

      $html = preg_replace('/[\t\n\r]/i', '', $req['body']);
      preg_match('/<table id=[\'"]tags-browser[\'"]>[\s\S]*?<\/table>/i',$html,$tmp);

      if (isset($tmp[0])) {
         $tags = $tmp[0]; 
         $tags = str_replace('/questions/tagged/','/tags/',$tags);
         $tags = str_replace('post-tag','label label-info',$tags);
      }


      preg_match('/<div class="pager fr"\s*>[\s\S]*?<\/div>/i',$html,$tmp);
      if (isset($tmp[0]))
        $pages = $tmp[0];

    }
    $f3->set('pages',$pages);
    $f3->set('css','tags.css');
    $f3->set('tags',$tags);
    $f3->set('site_name','<title>Android Enthusiasts</title>');
    $f3->set('site_desc','<meta name="description" content="The site allow user find the question about android and developmen"/>');
    $f3->set('site_keywords','<meta name="keywords" content="gp958.com android question"/>');


    echo View::instance()->render('tags.html');
  },2*3600
);
$f3->run();
